package com.sayimo.maker.user.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.common.MessageObject;
import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.user.dao.AreaInterfaceDao;
import com.sayimo.maker.user.dao.CityInterfaceDao;
import com.sayimo.maker.user.dao.ProvinceInterfaceDao;
import com.sayimo.maker.user.service.ProvinceInterfaceService;
import com.sayimo.maker.user.vo.Area;
import com.sayimo.maker.user.vo.City;
import com.sayimo.maker.user.vo.Province;


/**
 * @Project: schoolmallapi
 * @ClassName: ProvinceInterfaceImplService
 * @Description: 省市区Servie
 * @Author: 曹频辉
 * @Date: 日期：2015年12月24日 时间：上午11:08:43
 * @Version 1.0.0
 */
@Service(value = "provinceInterfaceImplService")
public class ProvinceInterfaceImplService implements ProvinceInterfaceService {

	private static Log logger = LogFactory.getLog(ProvinceInterfaceImplService.class);
	private static Map<String, Object> queryMap = new HashMap<>();
	@Autowired
	private ProvinceInterfaceDao provinceInterfaceImplDao;

	@Autowired
	private CityInterfaceDao cityInterfaceImplDao;

	@Autowired
	private AreaInterfaceDao areaInterfaceImplDao;

	/**
	 * @Title: queryProvince
	 * @Description: 查询所有省
	 * @return Map
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Object> queryProvince() throws ServiceException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List provinces = provinceInterfaceImplDao.getAllProvinces();
			if (provinces != null && provinces.size() >= 0) {
				map.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				map.put(Constant.DATA, provinces);
			} else {
				map.put(Constant.STATUS, Constant.STATUS_ERROR);
				map.put(Constant.ERROR_CODE, Constant.SELECT_ERROR);
			}
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
		return map;
	}

	/**
	 * @Title: queryCity
	 * @Description: 查询所有市
	 * @return Map
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Object> queryCity(String provinceCode) throws ServiceException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List cities = cityInterfaceImplDao.getAllCities(provinceCode);
			if (cities != null && cities.size() >= 0) {
				map.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				map.put(Constant.DATA, cities);
			} else {
				map.put(Constant.STATUS, Constant.STATUS_ERROR);
				map.put(Constant.ERROR_CODE, Constant.SELECT_ERROR);
			}
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
		return map;
	}

	/**
	 * @Title: queryArea
	 * @Description: 查询所有区
	 * @return Map
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, Object> queryArea(String cityCode) throws ServiceException {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List areas = areaInterfaceImplDao.getAllAreas(cityCode);
			if (areas != null && areas.size() >= 0) {
				map.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				map.put(Constant.DATA, areas);
			} else {
				map.put(Constant.STATUS, Constant.STATUS_ERROR);
				map.put(Constant.ERROR_CODE, Constant.SELECT_ERROR);
			}
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
		return map;
	}

	/**
	 * @Title: queryAddressByArea
	 * @Description: 根据区找到对应的省市区详细地址
	 * @return MessageObject
	 */
	@Override
	public Map<String, Object> queryAddressByArea(String areaCode) throws ServiceException {
		MessageObject messageObject = new MessageObject();
		Map<String, Object> map = new HashMap<String, Object>();
		String str = null;
		try {
			if (areaCode != null && !"".equals(areaCode) && areaCode.length() != 0) {
				Area area = areaInterfaceImplDao.getAreaByAreaCode(areaCode);
				if (area != null) {

					City city = cityInterfaceImplDao.getCityByCityCode(area.getCityCode());
					if (city != null) {

						Province province = provinceInterfaceImplDao.getProvinceByProvinceCode(city.getProvinceCode());
						if (province != null) {
							str = province.getName() + city.getName() + area.getName();
							messageObject.setKey(Constant.SUCCESS);
							messageObject.setObject(str);
							map.put(Constant.STATUS, Constant.STATUS_SUCCESS);
							map.put(Constant.DATA, str);
						} else {
							messageObject.setKey(Constant.ERROR_CODE);
							messageObject.setObject(Constant.SYSTEM_NO_DATA);
							map.put(Constant.STATUS, Constant.STATUS_ERROR);
							map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
						}
					} else {
						messageObject.setKey(Constant.ERROR_CODE);
						messageObject.setObject(Constant.SYSTEM_NO_DATA);
						map.put(Constant.STATUS, Constant.STATUS_ERROR);
						map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
					}
				} else {
					messageObject.setKey(Constant.ERROR_CODE);
					messageObject.setObject(Constant.SYSTEM_NO_DATA);
					map.put(Constant.STATUS, Constant.STATUS_ERROR);
					map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
				}
			} else {
				messageObject.setKey(Constant.ERROR_CODE);
				messageObject.setObject(Constant.SYSTEM_REQUEST_PARAM_ERROR);
				map.put(Constant.STATUS, Constant.STATUS_ERROR);
				map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
			}

		} catch (DaoException e) {
			logger.error(e);
			messageObject.setKey(Constant.ERROR_CODE);
			messageObject.setObject(Constant.SYSTEM_ERROR);
			map.put(Constant.STATUS, Constant.STATUS_ERROR);
			map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
		}
		return map;
	}

	/**
	 * @Package com.sayimo.school.mall.module.admin.service.impl
	 * @Author 龚益君
	 * @MethodName:queryAddressByAreaCode
	 * @Description:
	 * @param areaCode
	 * @return 将省市区做成数组
	 * @throws ServiceException
	 * @Date 2016年5月31日,下午6:22:02
	 */
	@Override
	public Map<String, Object> queryAddressByAreaCode(String areaCode) throws ServiceException {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			if (areaCode != null && !"".equals(areaCode) && areaCode.length() != 0) {
				Area area = areaInterfaceImplDao.getAreaByAreaCode(areaCode);
				if (area != null) {

					City city = cityInterfaceImplDao.getCityByCityCode(area.getCityCode());
					if (city != null) {

						Province province = provinceInterfaceImplDao.getProvinceByProvinceCode(city.getProvinceCode());
						if (province != null) {
							data.put("province", province.getName());
							data.put("city", city.getName());
							data.put("area", area.getName());
							map.put(Constant.STATUS, Constant.STATUS_SUCCESS);
							map.put(Constant.DATA, data);
						} else {
							map.put(Constant.STATUS, Constant.STATUS_ERROR);
							map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
						}
					} else {
						map.put(Constant.STATUS, Constant.STATUS_ERROR);
						map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
					}
				} else {
					map.put(Constant.STATUS, Constant.STATUS_ERROR);
					map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
				}
			} else {
				map.put(Constant.STATUS, Constant.STATUS_ERROR);
				map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
			}

		} catch (DaoException e) {
			logger.error(e);
			map.put(Constant.STATUS, Constant.STATUS_ERROR);
			map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
		}
		return map;
	}

	public ProvinceInterfaceDao getProvinceInterfaceImplDao() {
		return provinceInterfaceImplDao;
	}

	public void setProvinceInterfaceImplDao(ProvinceInterfaceDao provinceInterfaceImplDao) {
		this.provinceInterfaceImplDao = provinceInterfaceImplDao;
	}

	public CityInterfaceDao getCityInterfaceImplDao() {
		return cityInterfaceImplDao;
	}

	public void setCityInterfaceImplDao(CityInterfaceDao cityInterfaceImplDao) {
		this.cityInterfaceImplDao = cityInterfaceImplDao;
	}

	public AreaInterfaceDao getAreaInterfaceImplDao() {
		return areaInterfaceImplDao;
	}

	public void setAreaInterfaceImplDao(AreaInterfaceDao areaInterfaceImplDao) {
		this.areaInterfaceImplDao = areaInterfaceImplDao;
	}

	/**
	 * @Package com.sayimo.school.mall.module.admin.service
	 * @Author 龚益君
	 * @MethodName:queryAllAsJson
	 * @Description:获取所有的地址列表 为json数组
	 * @return
	 * @throws ServiceException
	 * @Date 2016年6月1日,上午10:01:32
	 */

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Map<String, Object> queryAllAsJson() throws ServiceException {
		try {
			List<Map<String, Object>> dataList = new ArrayList<>();
			if (queryMap.isEmpty()) {
				List provinceList = provinceInterfaceImplDao.getAllProvinces();
				for (Object object : provinceList) {
					Map<String, Object> map = (Map) object;
					Map<String, Object> provinceMap = new HashMap<>();
					provinceMap.put("id", map.get("provinceCode"));
					provinceMap.put("name", map.get("provinceName"));
					List cityList = cityInterfaceImplDao.getAllCities((String) map.get("provinceCode"));
					List<Map<String, Object>> dataList1 = new ArrayList<>();
					for (Object object1 : cityList) {
						Map<String, Object> map1 = (Map) object1;
						Map<String, Object> cityMap = new HashMap<>();
						cityMap.put("id", map1.get("cityCode"));
						cityMap.put("name", map1.get("cityName"));
						dataList1.add(cityMap);
						provinceMap.put("sub", dataList1);
						List areaList = areaInterfaceImplDao.getAllAreas((String) map1.get("cityCode"));
						List<Map<String, Object>> dataList2 = new ArrayList<>();
						for (Object object2 : areaList) {
							Map<String, Object> map2 = (Map) object2;
							Map<String, Object> areaMap = new HashMap<>();
							areaMap.put("id", map2.get("areaCode"));
							areaMap.put("name", map2.get("areaName"));
							dataList2.add(areaMap);
							cityMap.put("sub", dataList2);
						}

					}
					dataList.add(provinceMap);
				}
				queryMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				queryMap.put(Constant.DATA, dataList);
			}
		} catch (DaoException e) {
			logger.error(e);
			queryMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			queryMap.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
		}
		return queryMap;
	}

	public static Map<String, Object> getQueryMap() {
		return queryMap;
	}

	public static void setQueryMap(Map<String, Object> queryMap) {
		ProvinceInterfaceImplService.queryMap = queryMap;
	}
	
	
	@Override
	public Map queryCodeByarea(String code) throws ServiceException {
		
		try {
			return areaInterfaceImplDao.queryCodeByarea(code);
		} catch (DaoException e) {
			throw new ServiceException();
		}
	}
}
